package com.example.module.ai.listener;

import com.example.framework.websocket.core.sender.local.LocalNativeWebSocketMessageSender;
import com.example.framework.websocket.core.session.NativeWebSocketHandler;
import com.example.module.ai.event.NotifyEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class NotifyEventListener {

    @Qualifier("clueServer")
    @Autowired
    private NativeWebSocketHandler webSocketServer;

    private static final String MESSAGE_TYPE = "clue";

    @Async
    @EventListener
    public void onEventListener(NotifyEvent event) {
        LocalNativeWebSocketMessageSender messageSender = new LocalNativeWebSocketMessageSender(webSocketServer);
        switch (event.getIdType()) {
            case 1:
                messageSender.sendBySessionId(event.getId(), MESSAGE_TYPE, event.getMessage());
                break;
            case 2:
                messageSender.sendByUserId(event.getId(), MESSAGE_TYPE, event.getMessage());
                break;
            default:
                log.warn("无效的idType: {}", event.getIdType());
                break;
        }
    }
}
